<template>
  <div class="add-form">
    <el-dialog
      :title="text + pageTitle"
      :visible.sync="dialogFormVisible"
      @close="handleClose"
    >
      <el-form
        :rules="ruleInline"
        ref="dataForm"
        :model="formBase"
        label-position="left"
        label-width="120px"
        style="width: 400px; margin-left: 120px"
      >
        <el-form-item label="所属学科" prop="subjectName">
          <el-select placeholder="请选择" v-model="formBase.subjectName">
            <el-option v-for="item in tableData" :key="item.id" :label="item.subjectName" :value="item.subjectName"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="目录名称" prop="directoryName">
          <el-input v-model="formBase.directoryName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">{{ $t('table.cancel') }}</el-button>
        <el-button type="primary" @click="createData">{{
          $t('table.confirm')
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { update, add } from '@/api/hmmm/directorys.js'
export default {
  name: 'usersAdd',
  props: ['text', 'pageTitle', 'formBase', 'ruleInline', 'tableData'],
  data () {
    return {
      dialogFormVisible: false
    }
  },
  computed: {},
  methods: {
    // 弹层显示
    dialogFormV () {
      this.dialogFormVisible = true
    },
    // 弹层隐藏
    dialogFormH () {
      this.dialogFormVisible = false
    },
    // 退出
    handleClose () {
      this.$emit('handleCloseModal')
    },

    // 表单提交
    createData () {
      this.$refs.dataForm.validate(async valid => {
        if (valid) {
          const data = {
            ...this.formBase
          }
          if (this.formBase.id) {
            // 修改用户
            await update(data).then(() => {
              this.$emit('newDataes', this.formBase)
            })
          } else {
            // 添加用户
            await add(this.formBase).then(() => {
              this.$emit('newDataes', this.formBase)
            })
            this.dialogFormH()
          }
          this.$emit('handleCloseModal')
        } else {
          this.$Message.error('*号为必填项!')
        }
      })
    },
    fun () {
      this.$refs.dataForm.resetFields()
    }
  },
  // 挂载结束

  mounted: function () {},
  // 创建完毕状态
  created () {},
  // 组件更新
  updated: function () {}
}
</script>
<style>
.el-form--label-left .el-form-item__label {
  text-align: right;
}
.el-form-item--medium {
  margin-bottom: 22px;
}
.el-dialog__footer {
  text-align: center;
}
</style>
